package com.googlecode.allinclusive.fw.core.config;

import java.io.IOException;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import com.googlecode.allinclusive.fw.core.utils.ClassUtils;

/**
 * 全局配置文件读取类
 */
public class GlobalConfig {

	/* Log 实例对象*/
	private static final Log log = LogFactory.getLog(GlobalConfig.class);
	/* 默认全局配置文件路径*/
	public static final String CONFIG_FILE = ClassUtils.toProppertiesPath(GlobalConfig.class);
	/* 用户自定义配置文件路径*/
	public static final String CUSTOM_CONFIG_FILE = ClassUtils.toSimpleProppertiesPath(GlobalConfig.class);

	/* Config对象*/
	private static GlobalConfig _self = new GlobalConfig();
	
	/* 字符编码*/
	public static final String DEFAULT_ENCODING = GlobalConfig.getProperty("default.encoding", "UTF-8");
	
	/* 属性*/
	private Properties properties;
	
	/*
	 * 构造方法
	 */
	private GlobalConfig() {
		properties = load(CONFIG_FILE);
		properties.putAll(load(CUSTOM_CONFIG_FILE));
	}

	/*
	 * 返回指定键的值
	 */
    public static String getProperty(String key) {
    	return _self.properties.getProperty(key);
    }

	/*
	 * 返回指定键的值
	 */
    public static String getProperty(String key, String defaultValue) {
    	return _self.properties.getProperty(key, defaultValue);
    }    
    
	/*
	 * 返回指定键的值
	 */
    public static boolean getBoolean(String key) {
        Boolean b = getBoolean(key, null);
        if (b != null) {
            return b.booleanValue();
        } else {
        	return false;
        }
    }

	/*
	 * 返回指定键的值
	 */
    public static boolean getBoolean(String key, boolean defaultValue) {
        return getBoolean(key, new Boolean(defaultValue)).booleanValue();
    }

	/*
	 * 返回指定键的值
	 */
    public static Boolean getBoolean(String key, Boolean defaultValue) {
    	String value = getProperty(key);
    	if (StringUtils.isBlank(value)) {
    		return defaultValue;
    	}
    	if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes")) {
    		return true;
    	} else if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no")) {
    		return false;
    	} else {
    		return defaultValue;
    	}
    }
	
    /**
     * 加载指定的配置文件
     */
	public static Properties load(String resourceName) {
		return load(resourceName, (ClassLoader) null);
	}
	
	/**
	 * 加载指定的配置文件
	 * @param resourceName 配置文件
	 * @param classLoader
	 * @return
	 */
	public static Properties load(String resourceName, ClassLoader classLoader) {
		log.debug(resourceName + " loading...");
		
		// 类加载器
		if (classLoader == null) {
			classLoader = ClassUtils.getDefaultClassLoader();
		}
		
		// 加载属性文件
		Properties p = new Properties();
		try {
			p = PropertiesLoaderUtils.loadProperties(new ClassPathResource(resourceName));
		} catch (IOException ioe) {
			log.error(ioe);
		} finally {
		}
		
		return p;
	}
}
